package Com.Hupochuan.Uil.Do;

import java.util.List;
import java.util.Vector;

import Com.Hupochuan.Dal.Tab.ActorTab;
import Com.Hupochuan.Dal.Tab.DirectorTab;
import Com.Hupochuan.Dal.Tab.MovieTab;
import Com.Hupochuan.Dal.Tab.PageMovieListTab;
import Com.Hupochuan.Uil.Servlet.DoRunServlet;
import Com.Hupochuan.Uil.Util.MemoryPond;
import Com.Hupochuan.Vo.Actor;
import Com.Hupochuan.Vo.AreaSort;
import Com.Hupochuan.Vo.Director;
import Com.Hupochuan.Vo.Movie;
import Com.Hupochuan.Vo.PageMovieList;
import Com.Hupochuan.Vo.Type;
import Com.Hupochuan.Vo.Year;
import Com.Tester.Tester;

@SuppressWarnings("serial")
public class MovieListDo extends DoRunServlet{
	
	//页长
	private long pageSize = 20;
	
	public void doRun(){
		
		//打印测试
		Tester.print("打印测试 - MovieListDo");
		
		//查找地址栏参数
		long yearId = this.getDoTool().findLongPathPara("y");
		long areaSortId = this.getDoTool().findLongPathPara("s");
		long typeId = this.getDoTool().findLongPathPara("t");
		long directorId = this.getDoTool().findLongPathPara("d");
		long actorId = this.getDoTool().findLongPathPara("a");
		long pageNo = this.getDoTool().findLongPathPara("p");
		String key = this.getDoTool().findUrlChinesePathPara("k_");
		
		//页号转换
		if(pageNo < 1){
			pageNo = 1;
		}
		
		PageMovieList pageMovieList = null;
		
		//如果没有关键字
		if(key.equals("")){
			//查找内存电影列表页面
			pageMovieList = MemoryPond.getObject().getPageMovieList(yearId, areaSortId, typeId, directorId, actorId, pageNo);
			
			//如果找到页 并且 不需要更新
			if(pageMovieList != null && pageMovieList.getIsUpdate() == 0){
				//转发静态页面
				this.getDoTool().forward(pageMovieList.getUrl());
				return;
			}
		}
		
		//验证年份参数
		Year year = MemoryPond.getObject().getYear(yearId);
		if(yearId != 0 && year == null){
			//发送错误
			this.getDoTool().sendError(404);
			return;
		}
		
		//验证地区分类参数
		if(areaSortId != 0 && MemoryPond.getObject().getAreaSort(areaSortId) == null){
			//发送错误
			this.getDoTool().sendError(404);
			return;
		}
		
		//验证类型参数
		if(typeId != 0 && MemoryPond.getObject().getType(typeId) == null){
			//发送错误
			this.getDoTool().sendError(404);
			return;
		}
		
		//内存池导演列表 和 显示导演列表
		List<Director> memoryDirectorList = MemoryPond.getObject().getDirectorList(yearId,areaSortId,typeId);
		List<Director> directorList = new Vector<Director>();
		
		//没有找到导演的旗帜
		boolean notDirector = true;
		
		//遍历导演列表
		for(Director director : memoryDirectorList){
			if(director.getId() == directorId){
				notDirector = false;
			}
			directorList.add(director);
		}
		
		//验证导演参数
		if(directorId != 0){
			//如果内存池导演列表中没有找到这个导演
			if(notDirector){
				Director director = DirectorTab.getObject().findDirector(directorId);
				if(director == null){
					//发送错误
					this.getDoTool().sendError(404);
					return;
				}
				else{
					directorList.add(director);
				}
			}
		}
		
		//内存池演员列表 和 显示演员列表
		List<Actor> memoryActorList = MemoryPond.getObject().getActorList(yearId,areaSortId,typeId,directorId);
		List<Actor> actorList = new Vector<Actor>();
		
		//没有找到演员的旗帜
		boolean notActor = true;
		
		//遍历演员列表
		for(Actor actor : memoryActorList){
			if(actor.getId() == actorId){
				notActor = false;
			}
			actorList.add(actor);
		}
		//验证演员参数
		if(actorId != 0){	
			//如果内存池演员列表中没有找到这个演员
			if(notActor){
				Actor actor = ActorTab.getObject().findActor(actorId);
				if(actor == null){
					//发送错误
					this.getDoTool().sendError(404);
					return;
				}
				else{
					actorList.add(actor);
				}
			}
		}
		
		//获得内存参数
		List<Year> yearList = MemoryPond.getObject().getYearList();
		List<AreaSort> areaSortList = MemoryPond.getObject().getAreaSortList();
		List<Type> typeList = MemoryPond.getObject().getTypeList();
		List<Movie> tswkList = MemoryPond.getObject().getTswkList();
		
		//查询电影数量
		long infoCount = MovieTab.getObject().findMovieCount(key,year,areaSortId,typeId,directorId,actorId);
		
		//初始化页数
		long pageCount = 1;
		
		//计算页数
		if(infoCount != 0){
			//如果信息数量正好满页
			if(infoCount % this.pageSize == 0){
				pageCount = infoCount / this.pageSize; 
			}
			//如果信息数量不满页
			else{
				pageCount = infoCount / this.pageSize + 1;
			}
		}
		
		//验证页号参数
		if(pageNo > pageCount){
			//发送错误
			this.getDoTool().sendError(404);
			return;
		}
		
		//查询电影列表
		List<Movie> movieList = MovieTab.getObject().findMovieList(key,year,areaSortId,typeId,directorId,actorId,pageNo,this.pageSize);;
		
		//设置参数
		this.getDoTool().setAttr("key", key);
		this.getDoTool().setAttr("yearId", yearId);
		this.getDoTool().setAttr("areaSortId", areaSortId);
		this.getDoTool().setAttr("typeId", typeId);
		this.getDoTool().setAttr("directorId", directorId);
		this.getDoTool().setAttr("actorId", actorId);
		this.getDoTool().setAttr("yearList", yearList);
		this.getDoTool().setAttr("areaSortList", areaSortList);
		this.getDoTool().setAttr("typeList", typeList);
		this.getDoTool().setAttr("directorList", directorList);
		this.getDoTool().setAttr("actorList", actorList);
		this.getDoTool().setAttr("movieList", movieList);
		this.getDoTool().setAttr("tswkList", tswkList);
		this.getDoTool().setAttr("infoCount",infoCount);
		this.getDoTool().setAttr("pageNo", pageNo);
		this.getDoTool().setAttr("pageCount", pageCount);
		
		//转发页面
		this.getDoTool().doForward("/jsp/movieList.jsp");
		
		//获得html内容
		String htmlContent = this.getDoTool().htmlEncode(this.getDoTool().getContent());
		
		//发送信息
		this.getDoTool().sendMsg(htmlContent);
		
		//如果不是搜索
		if(key.equals("")){
			//没有保存此页面
			if(pageMovieList == null){
				//添加电影列表页面
				PageMovieListTab.getObject().appendData(yearId, areaSortId, typeId, directorId, actorId, pageNo);
				
				//再次查找电影列表页面
				pageMovieList = PageMovieListTab.getObject().findPageMovieList(yearId, areaSortId, typeId, directorId, actorId, pageNo);
			}
			
			//需要更新此页面
			if(pageMovieList.getIsUpdate() == 1){
				//移动临时文件到 标准目录中
				StringBuffer urlBuffer = new StringBuffer();
				urlBuffer.append("/html/movieList/");
				urlBuffer.append(String.format("%012d", pageMovieList.getId()));//补齐12位ID
				urlBuffer.append(".html");
				
				//执行保存文件
				if(this.getDoTool().saveHtml(urlBuffer.toString(), htmlContent)){
					//修改电影列表页面
					PageMovieListTab.getObject().updateData(urlBuffer.toString(), yearId, areaSortId, typeId, directorId, actorId, pageNo);
					
					//新的静态页面路径改变了 那就删除以前的
					if(!urlBuffer.toString().equals(pageMovieList.getUrl())){
						this.getDoTool().deleteFile(pageMovieList.getUrl());
					}
				}
			}
		}
	}
}
